//
//  WCUserService.m
//  WeboCon
//
//  Created by black sun on 12-3-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "WCUserService.h"

@implementation WCUserService

-(void)getMyUid{
    NSString *url = [NSString stringWithFormat:@"%@account/get_uid.json",Sina_Base_URL_V2];
    ASIHTTPRequest *request = [WCApiRequest getRequestWithApiURL:url needOAuth:YES];
    request.userInfo = [NSDictionary dictionaryWithObject:GetMyUid forKey:kRequestTypeKey];
    request.delegate = self;
    [workingRequest addObject:request];
    [request startSynchronous];
}
-(void)getUserByUid:(NSNumber *)uid{
    NSString *url = [NSString stringWithFormat:@"%@users/show.json",Sina_Base_URL_V2];
    NSString *newUrl = [url appendQueryParameter:[NSDictionary dictionaryWithObject:[uid stringValue] forKey:@"uid"]];
    ASIHTTPRequest *request = [WCApiRequest getRequestWithApiURL:newUrl needOAuth:YES];
    request.userInfo = [NSDictionary dictionaryWithObject:GetUserByUid forKey:kRequestTypeKey];
    request.delegate = self;
    [workingRequest addObject:request];
    [request startSynchronous];
}
-(void)getUserByScreenName:(NSString *)screen_name{
    NSString *url = [NSString stringWithFormat:@"%@users/show.json",Sina_Base_URL_V2];
    NSString *newUrl = [url appendQueryParameter:[NSDictionary dictionaryWithObject:screen_name forKey:@"screen_name"]];
    ASIHTTPRequest *request = [WCApiRequest getRequestWithApiURL:newUrl needOAuth:YES];
    request.userInfo = [NSDictionary dictionaryWithObject:GetUserByScreenName forKey:kRequestTypeKey];
    request.delegate = self;
    [workingRequest addObject:request];
    [request startSynchronous];
}

-(void)requestSuccess:(ASIHTTPRequest *)request withJSON:(NSDictionary *)json{
    NSString *action = [request.userInfo objectForKey:kRequestTypeKey];
    if ([action isEqualToString:GetUserByUid] || [action isEqualToString:GetUserByScreenName]) {
        NSNumber *uid = [NSNumber numberWithLong:[[json objectForKey:@"id"] longValue]];
        User *user = [User entityWithId:uid]?:[User entityFromCoreDataContext];
        [WCModelHelper populateValues:user fromDictionary:json];
        [[WCCoreDataContext sharedInstance] saveContext];
        [self.delegate requestDidFinishedWithResult:user andUserInfo:request.userInfo];
    }
}

@end
